﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ScaffoldIt.Configuration
{
    public class Parameter : IDescriptiveElement
    {
        public const string PARAM_STRING = "string";
        public const string PARAM_BOOL = "bool";
        public const string PARAM_CHOICE = "choice";

        public static string[] ParamTypes = new string[] { PARAM_STRING, PARAM_BOOL, PARAM_CHOICE };

        public static string ParamTypesList()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var paramType in ParamTypes)
            {
                if (sb.Length > 0) sb.Append(", ");
                sb.Append(paramType);
            }
            return sb.ToString();
        }

        [XmlAttribute]
        public string Code { get; set; }
        
        [XmlAttribute]
        public string Title { get; set; }

        [XmlAttribute]
        public string Description { get; set; }

        [XmlAttribute]
        public string Type { get; set; }

        [XmlAttribute]
        public string Choices { get; set; }

        [XmlAttribute]
        public string Value { get; set; }

        [XmlAttribute]
        public bool IsGlobal { get; set; }

        public bool ValidateType()
        {
            if (!ParamTypes.Any(t => t == this.Type))
                return false;

            return true;
        }

        public static bool ValidateCode(string code)
        {
            if (string.IsNullOrEmpty(code)) return false;

            foreach (var ch in code)
            {
                if (!Char.IsLetterOrDigit(ch))
                    return false;
            }

            return true;
        }

        public bool ValidateCode()
        {
            return ValidateCode(this.Code);
        }

        public object GetValue()
        {
            if (this.Type == PARAM_BOOL) 
                return string.Compare(Value, "true", StringComparison.OrdinalIgnoreCase) == 0;

            if (Value == null) return string.Empty;

            return Value;
        }
    }
}
